package com.sgb.trip.view.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.link.entity.PickSelectEntity;
import com.sgb.link.view.adapter.IpPickSelectAdapter;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripPopwindowFilterFlightMoreBinding;
import com.sgb.trip.entity.FlightLineSearchEntity;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class FlightMoreFilterPopWindow extends BasePopWindow implements View.OnClickListener {
    TripPopwindowFilterFlightMoreBinding binding;

    private RecyclerView timeRecyclerView;
    private RecyclerView positionRecyclerView;
    private MaxRecyclerView companyRecyclerView;

    private TextView tv_reset;
    private TextView tv_sure;
    private View top_transparent_layout;

    private IpPickSelectAdapter timeAdapter;
    private IpPickSelectAdapter positionAdapter;
    private IpPickSelectAdapter companyAdapter;

    private List<PickSelectEntity> companyList;//航空公司

    private FlightLineSearchEntity mEntity;

    protected OnClickCallBack onClickCallBack;

    public FlightMoreFilterPopWindow(Context context) {
        super(context, DIR_RIGHT);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int popLayout() {
        return R.layout.trip_popwindow_filter_flight_more;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        binding = (TripPopwindowFilterFlightMoreBinding) holder.getmBinding();
        mEntity = new FlightLineSearchEntity();

        timeRecyclerView = holder.getPop().findViewById(R.id.time_recyclerView);
        positionRecyclerView = holder.getPop().findViewById(R.id.position_recyclerView);
        companyRecyclerView = holder.getPop().findViewById(R.id.company_list);

        tv_reset = holder.getPop().findViewById(R.id.reset_step);
        tv_sure = holder.getPop().findViewById(R.id.sure_step);
        top_transparent_layout = holder.getPop().findViewById(R.id.top_transparent_layout);

        tv_reset.setOnClickListener(this::onClick);
        tv_sure.setOnClickListener(this::onClick);
        top_transparent_layout.setOnClickListener(this::onClick);

        binding.companyMore.setOnClickListener((View v) -> {
            if (companyList != null && companyList.size() > 0)
                companyAdapter.setDatas(companyAdapter.getDatas().size() > 9 ? companyList.subList(0, 9) : companyList);
            binding.companyMore.setImageResource(companyAdapter.getDatas().size() > 9 ? R.mipmap.common_zhankai : R.mipmap.common_shouqi);
        });

        initTimeRecyclerView();
        initPositionRecyclerView();
        initCompanyRecyclerView();
    }

    /**
     * 初始化起飞时间
     */
    private void initTimeRecyclerView() {
        List<PickSelectEntity> datas = new ArrayList<>();
        datas.add(new PickSelectEntity("1", "00:00-08:00"));
        datas.add(new PickSelectEntity("2", "08:00-14:00"));
        datas.add(new PickSelectEntity("3", "14:00-19:00"));
        datas.add(new PickSelectEntity("4", "19:00-24:00"));
        timeAdapter = new IpPickSelectAdapter(mContext, datas);
        timeAdapter.setMultiSelect(true);
        timeAdapter.setItemSelect(false);
        timeAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> ipPickSelectEntities, int position) {
                if (ipPickSelectEntities != null && ipPickSelectEntities.size() > 0) {
                    StringBuilder choose = new StringBuilder();
                    List<String> listChoose = new ArrayList<>();
                    for (PickSelectEntity entity : ipPickSelectEntities) {
                        choose.append(entity.getName()).append(",");
                        listChoose.add(entity.getValue());
                    }
                    String result = choose.toString().substring(0, choose.toString().length() - 1);
                    binding.choosedTime.setText(result);
                    mEntity.setFromTime(result);
                } else {
                    binding.choosedTime.setText("");
                    mEntity.setFromTime("");
                }
            }
        });
        timeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        timeRecyclerView.setAdapter(timeAdapter);
    }

    /**
     * 初始化仓位
     */
    private void initPositionRecyclerView() {
        List<PickSelectEntity> datas = new ArrayList<>();
        datas.add(new PickSelectEntity("1", "经济舱"));
        datas.add(new PickSelectEntity("2", "公务舱"));
        datas.add(new PickSelectEntity("3", "头等舱"));
        positionAdapter = new IpPickSelectAdapter(mContext, datas);
        positionAdapter.setMultiSelect(true);
        positionAdapter.setItemSelect(false);
        positionAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> ipPickSelectEntities, int position) {
                if (ipPickSelectEntities != null && ipPickSelectEntities.size() > 0) {
                    StringBuilder choose = new StringBuilder();
                    List<String> listChoose = new ArrayList<>();
                    for (PickSelectEntity entity : ipPickSelectEntities) {
                        choose.append(entity.getName()).append(",");
                        listChoose.add(entity.getValue());
                    }
                    binding.choosedPosition.setText(choose.toString().substring(0, choose.toString().length() - 1));

                    mEntity.setCabinLevel(listChoose);
                } else {
                    binding.choosedPosition.setText("");
                    mEntity.setCabinLevel(new ArrayList<>());
                }
            }
        });
        positionRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        positionRecyclerView.setAdapter(positionAdapter);
    }

    /**
     * 初始化航空公司
     */
    private void initCompanyRecyclerView() {
        companyAdapter = new IpPickSelectAdapter(mContext, null);
        companyAdapter.setMultiSelect(true);
        companyAdapter.setItemSelect(false);
        companyAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<PickSelectEntity>() {
            @Override
            public void onItemMultiClick(List<PickSelectEntity> ipPickSelectEntities, int position) {
                if (ipPickSelectEntities != null && ipPickSelectEntities.size() > 0) {
                    StringBuilder choose = new StringBuilder();
                    List<String> listChoose = new ArrayList<>();
                    for (PickSelectEntity entity : ipPickSelectEntities) {
                        choose.append(entity.getName()).append(",");
                        listChoose.add(entity.getValue());
                    }
                    binding.choosedCompany.setText(choose.toString().substring(0, choose.toString().length() - 1));
                    mEntity.setAirlineCode(listChoose);
                } else {
                    binding.choosedCompany.setText("");
                    mEntity.setAirlineCode(new ArrayList<>());
                }
            }
        });
        companyRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        companyRecyclerView.setAdapter(companyAdapter);
    }

    /**
     * 设置航空公司数据
     *
     * @param datas
     */
    public void setCompanyDatas(List<PickSelectEntity> datas) {
        companyList = new ArrayList<>();
        companyList = datas;
        companyAdapter.setDatas(companyList);
        if (companyList.size() > 9) {
            binding.companyMore.setVisibility(View.VISIBLE);
            binding.companyMore.setImageResource(R.mipmap.common_shouqi);
            companyAdapter.setDatas(companyList.subList(0, 9));
        } else {
            binding.companyMore.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.reset_step) {//重置
            resetWindow();
        } else if (id == R.id.sure_step) {//确认
            if (mEntity != null && onClickCallBack != null) {
                onClickCallBack.result(mEntity);
                dismiss();
            }
        }
    }

    /**
     * 重置窗口
     */
    public void resetWindow() {
        binding.choosedTime.setText("");
        binding.choosedPosition.setText("");
        binding.choosedCompany.setText("");
        if (null != companyList && companyList.size() > 0) {
            companyAdapter.setDatas(companyList.size() > 9 ? companyList.subList(0, 9) : companyList);
        }
        binding.companyMore.setImageResource(R.mipmap.common_shouqi);
        timeAdapter.setDefItem(-1);
        positionAdapter.setDefItem(-1);
        companyAdapter.setDefItem(-1);
        timeAdapter.resetCheckDatas();
        positionAdapter.resetCheckDatas();
        companyAdapter.resetCheckDatas();
        mEntity.setFromTime("");
        mEntity.setCabinLevel(new ArrayList<>());
        mEntity.setAirlineCode(new ArrayList<>());
    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public interface OnClickCallBack {
        void result(FlightLineSearchEntity entity);
    }
}
